{{ $here           := .RelPermalink -}}
{{ $currentSection := .CurrentSection -}}
{{ $docSections := (index (where site.Sections "Section" "docs") 0).Sections -}}

<div class="nav is-sticky">
  {{ range $docSections -}}
  {{ $children := .RegularPages -}}
  {{ if eq .Params.nav_children "none" -}}
    {{ $children = false -}}
  {{ else if hasPrefix .Params.nav_children "section" -}}
    {{ $children = .Sections -}}
  {{ else if hasPrefix .Params.nav_children "pages" -}}
    {{ $children = .Pages -}}
  {{ end -}}
  {{ $isThisSection := hasPrefix $currentSection.RelPermalink .CurrentSection.RelPermalink -}}
  <div class="nav-section" x-data="{ open: {{ $isThisSection }} }">
    {{ $isActive := (or $isThisSection (eq $here .RelPermalink)) -}}
    {{ $title := .Params.short | default .Title | upper -}}
    <nav class="level">
      <div class="level-left">
        <a class="nav-section-title is-size-5 is-size-6-mobile{{ if $isActive }} is-active{{ end }}" href="{{ .RelPermalink }}">
          {{ $title }}
        </a>
      </div>

      {{ if $children -}}
      <div class="level-right">
        <span class="level-item" @click="open = !open">
          <i class="fas has-text-grey" :class="{ 'fa-chevron-down': !open, 'fa-chevron-up': open }"></i>
        </span>
      </div>
      {{ end -}}
    </nav>

    {{ with $children -}}
    <ul class="nav-section-links" x-show="open">
      {{ range . -}}
      {{ $children := .RegularPages -}}
      {{ $isThisSection := hasPrefix $currentSection.RelPermalink .CurrentSection.RelPermalink -}}
      {{ $isActive := eq $here .RelPermalink -}}
      {{ if $children -}}
        {{ $isActive = or $isActive $isThisSection -}}
      {{ end -}}
      <li class="nav-section-link" x-data="{ open: {{ $isThisSection }} }">
        <nav class="level">
          <div class="level-left{{ if $isActive }} is-active{{ end }}">
            <a href="{{ .RelPermalink }}">
              {{ .Params.short | default .Title }}
            </a>
          </div>
          {{ if $children -}}
            <div class="level-right">
              <span class="level-item" @click="open = !open">
                <i class="fas has-text-grey" :class="{ 'fa-chevron-down': !open, 'fa-chevron-up': open }"></i>
              </span>
            </div>
          {{ end -}}
        </nav>
        {{ if $children -}}
          <ul class="nav-section-links" x-show="open">
            {{ range $children -}}
              {{ $isActive := eq $here .RelPermalink -}}
              {{ $isRedirected :=  findRE "^/docs/languages/.+?/(api|daily-builds)(/.*)?$" .RelPermalink -}}
              {{ $isExternal := or (hasPrefix .RelPermalink "http") $isRedirected -}}
              <li class="nav-section-link{{ if $isActive }} is-active{{ end }}">
                <a href="{{ .RelPermalink }}"
                  {{- if $isExternal }} target="_blank" rel="noopener"{{ end -}}
                >
                  {{- .Params.short | default .Title -}}
                  {{ if $isExternal -}}
                    <i class="fas fa-external-link-alt"></i>
                  {{- end -}}
                </a>
              </li>
            {{ end -}}
          </ul>
        {{ end -}}
      </li>
      {{ end -}}
    </ul>
    {{ end -}}
  </div>
  {{ end -}}
</div>
